/**
 * 订单管理管理初始化
 */
var TOrder = {
    id: "TOrderTableDis",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TOrder.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {title: '日期', field: 'order_date', width: 130,visible: true, align: 'center', valign: 'middle', fixed: 'left'},
        {title: '编号', field: 'original_num', width: 100,visible: true, align: 'center', valign: 'middle', fixed: 'left'},
        {title: '收货人', field: 'consignee', visible: true, width: 100,align: 'center', valign: 'middle', fixed: 'left'},
        {title: '物品名称', field: 'goodsName', visible: true, width: 120,align: 'center', valign: 'middle'},
        {title: '收货电话', field: 'receivingPhone', visible: true, width: 120,align: 'center', valign: 'middle'},
        {title: '件数', field: 'goodsPiece', visible: true, width: 80,align: 'center', valign: 'middle'},
        {title: '提付款', field: 'payment',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '现付费', field: 'scene_money',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '运单状态', field: 'statusName',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '通知状态', field: 'notice', visible: true,width: 120, align: 'center', valign: 'middle'},
        {title: '回单状态', field: 'receiptStatus', width: 120,visible: true, align: 'center', valign: 'middle'},
        {title: '回单要求', field: 'receipt', visible: true, width: 120,align: 'center', valign: 'middle'},
        {title: '重量', field: 'goodsWeight', visible: true,width: 80, align: 'center', valign: 'middle'},
        {title: '体积', field: 'goodsVolume', visible: true,width: 80, align: 'center', valign: 'middle'},
        {title: '备注', field: 'mark',width: 180, visible: true, align: 'center', valign: 'middle'},
        {title: '收货地址', field: 'receivingAddress',width: 180, visible: true, align: 'center', valign: 'middle'}
    ];
};

TOrder.check = function () {

    var selected = mTable.checkStatus(TOrder.id);
    if (selected.data.length == 0 || selected.data.length > 1) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TOrder.seItem = selected.data[0];
        return true;
    }
};

/**
 * 检查是否选中
 */
TOrder.checks = function () {
    var selected = mTable.checkStatus(TOrder.id);
    if (selected.data.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }
    return true;
};
/**
 * 点击添加订单管理
 */
TOrder.openAddTOrder = function () {
    var index = layer.open({
        type: 2,
        title: '添加订单管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tOrder/tOrder_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看订单管理详情
 */
TOrder.openTOrderDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '订单管理详情',
            area: ['800px', '480px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tOrder/tOrder_update/' + TOrder.seItem.id
        });
        this.layerIndex = index;
    }
};
/**
 * 打开查看订单管理详情
 */
TOrder.theLibrary = function () {
    // if (this.check()) {
    //     if(TOrder.seItem.statusName=='审核中'||TOrder.seItem.statusName=='待出库'){
    //         var index = layer.open({
    //             type: 2,
    //             title: '订单出库',
    //             area: ['1150px', '450px'], //宽高
    //             fix: false, //不固定
    //             maxmin: true,
    //             content: Feng.ctxPath + '/tOrder/the_library/' + TOrder.seItem.id
    //         });
    //         layer.full(index);
    //         this.layerIndex = index;
    //     }else{
    //         window.open(Feng.ctxPath + "/tOrder/the_print/"+TOrder.seItem.id+"?tOrderId="+TOrder.seItem.id);
    //
    //     }
    //
    // }

    var selected = mTable.checkStatus(TOrder.id);
    var ids = new Array();
    var idss = new Array();
    for (var i = 0; i < selected.data.length; i++) {
        if(selected.data[i].status=='1'){
            ids.push(selected.data[i].id);
        }else if(selected.data[i].status=='2'){
            idss.push(selected.data[i].id);
        }

    }
    if(idss.length>0&&ids.length>0){
        Feng.info("出库状态和待出库状态不能同时操作！");
        return;
    }
    if (this.checks()) {
        if(ids.length>0){
            var index = layer.open({
                type: 2,
                shade:0.01,
                title: '订单出库',
                area: ['1150px', '450px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/tOrder/getDistributionOrders?ids='+ids.join(",")
            });
            layer.full(index);
            this.layerIndex = index;
        }else{
            window.open(Feng.ctxPath + "/tOrder/dis_print?ids="+idss.join(","));

        }

    }
};

/**
 * 回单处理
 */
TOrder.orderReceipt = function () {
    if (this.checks()) {
        var selected = mTable.checkStatus(TOrder.id);
        var ids=new Array();
        for (var i = 0; i < selected.data.length; i++) {
            if(selected.data[i].statusName='已出库'){
                ids.push(selected.data[i].id);
            }
        }
        layer.confirm('回单处理', {
            btn: ['取消', '已处理']
        }, function(index, layero){
            layer.close(layero);
        }, function(index, layero){
            var ajax = new $ax(Feng.ctxPath + "/tOrder/orderReceipt", function (data) {
                Feng.success("处理成功!");
                aTable.refresh();
                layer.close(index)
            }, function (data) {
                Feng.error("处理失败!" + data.responseJSON.message + "!");
                layer.close(layero);
            });
            ajax.set("tOrderId", ids.join(","));
            ajax.start();

        });

    }
};


/**
 * 通知
 */
TOrder.orderTipStaus = function () {

    if (this.check()) {
        layer.confirm('手工通知？', {
            btn: ['取消', '已联系','无法联系']
            ,btn3: function(index, layero){
                tip('1',index);
            }
        }, function(index, layero){
            layer.close(index)
        }, function(index){
            tip('0',index);
        });

    }
};
function tip(mVal,obj){
    var ajax = new $ax(Feng.ctxPath + "/tOrder/orderTipStaus", function (data) {
        Feng.success("除了成功!");
        aTable.refresh();

    }, function (data) {
        Feng.error("处理失败!" + data.responseJSON.message + "!");
    });
    ajax.set("tOrderId", TOrder.seItem);
    ajax.set("isOut", mVal);
    ajax.start();
    layer.close(obj);
}

/**
 * 订单出库管理
 */
TOrder.checkOrder = function () {

    var selected = mTable.checkStatus(TOrder.id);
    var ids=new Array();
    for (var i = 0; i < selected.data.length; i++) {
        ids.push(selected[i].id);
    }
    if (this.checks()) {
        var ajax = new $ax(Feng.ctxPath + "/tOrder/checkOrder", function (data) {
            Feng.success("审核成功!");
            aTable.refresh();
        }, function (data) {
            Feng.error("审核失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids.join(","));
        ajax.start();
    }
};
/**
 * 查询订单管理列表
 */
TOrder.search = function () {
    var receipt_status=$("#receipt_status").val()+"";
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['status'] = $("#status").val()+"";
    queryData['receipt_status'] = receipt_status;
    if ($('#is_out').is(":checked")) {
        queryData['is_out'] = 1;
    }else{
        queryData['is_out'] = 0;
    }

    aTable.refresh(queryData);
};

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TOrder.initColumn();
    aTable = new LYTable(TOrder.id,  "/tOrder/outList?flag=1", defaultColunms);
    // table.setPaginationType("client");
    layui.use('table', function () {
        mTable = layui.table;
        TOrder.table = aTable.init(mTable);
    })

    $('#receipt_status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"回单状态"
    });
    $('#status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"出库状态"
    });
});
